<template>
  <div class="container">
    <div class="app-container">
      <el-tree :data="depts" default-expand-all :props="defaultProps" @node-click="handleNodeClick">
        <!-- 节点结构 -->
        <template v-slot="{ data:res }">
          <el-row style="width: 100%;height: 40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ res.name }}</el-col>
            <el-col :span="3">
              <!-- 管理员名字 -->
              <span style="margin-right: 10px;">{{ res.managerName }}</span>
              <!-- 下拉菜单 -->
              <el-dropdown @command="handleCommand($event, res.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>

      </el-tree>
      <!-- 弹窗组件  .sync(了解即可 vue3废弃了) 组件内部直接可以改变数据 父组件数据自动跟着变化-->
      <AddDept ref="AddDept" :show-dialog.sync="showDialog" :current-node-id="currentNodeId" @updateDepartment="getDepartmentAPI" />
    </div>
  </div>
</template>
<script>
import { getDepartment, delDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: { AddDept },
  data() {
    return {
      // 使用树形插件的时候  要注意label  和 children 的值跟 data中的对应上
      depts: [{
        label: '传智教育',
        children: [
          {
            label: '总裁办'
          },
          {
            label: '行政部'
          },
          {
            label: '人事部',
            children: [
              {
                label: '财务核算部'
              },
              {
                label: '税务管理部'
              },
              {
                label: '薪资管理部'
              }
            ]
          }
        ]
      }],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 设置弹窗显示关闭
      currentNodeId: null // 设置记录部门的值
    }
  },
  created() {
    this.getDepartmentAPI()
  },
  methods: {
    handleNodeClick(data) { // 节点被点击时的回调
    },
    async getDepartmentAPI() { // 调用组织结构列表
      const res = await getDepartment()
      this.depts = transListToTreeData(res, 0)
    },
    // 点击下拉框触发内容
    handleCommand(val, id) {
      if (val === 'add') {
        // 新增
        this.showDialog = true
        this.currentNodeId = id
      } else if (val === 'edit') {
        // 编辑
        this.showDialog = true
        this.currentNodeId = id
        this.$nextTick(() => {
          this.$refs.AddDept.getDepartmentDetail()
        })
      } else {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await delDepartment(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getDepartmentAPI()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }
  }
}
</script>

<style scoped>
.app-container {
  /* padding: 30px 140px; */
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;

}
</style>
